﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SilverCrmSoap.CrmSdk;


namespace Linq2Crm.Request.SpecificRequest
{
	public sealed class ManyToManyRelationshipRequest : RequestBase
	{
		private ManyToManyRelationshipRequest()
			: base(null)
		{
		}

		internal string ManyToManyRelationshipName { get; private set; }

		public Action<ManyToManyRelationshipResponse> Callback { get; set; }

		internal List<Guid> FEntityFilter { get; private set; }
		internal List<Guid> SEntityFilter { get; private set; }

		internal ManyToManyRelationshipMetadata InnerMetadata { get; set; }

		#region Overrides of RequestBase
		internal override void Execute()
		{
			DomainObjectContext.Instance.ExecuteRelationshipRequest(this);
		}

		internal override async Task<ResponseBase> ExecuteAsync()
		{
			var response = await DomainObjectContext.Instance.ExecuteRelationshipRequestAsync(this);
			return response;
		}

		#endregion

		public static ManyToManyRelationshipRequest Create(string relationShipName, List<Guid> firstEntityFilter, List<Guid> secondEntityFilter = null)
		{
			return new ManyToManyRelationshipRequest
			       	{
			       		ManyToManyRelationshipName = relationShipName,
			       		FEntityFilter = firstEntityFilter,
			       		SEntityFilter = secondEntityFilter
			       	};
		}
	}
}